package com.ruoyi.doorcontrol.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.doorcontrol.domain.ZyCar;
import com.ruoyi.doorcontrol.service.IZyCarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 公司车辆Controller
 *
 * @author zhang
 * @date 2023-12-29
 */
@RestController
@RequestMapping("/system/zycar")
public class ZyCarController extends BaseController
{
    @Autowired
    private IZyCarService zyCarService;

    @Value("${ruoyi.serverip}")
    private String serverip;

    /**
     * 查询公司车辆列表
     */
    @PreAuthorize("@ss.hasPermi('system:zycar:list')")
    @GetMapping("/list")
    public TableDataInfo list(ZyCar zyCar)
    {
        startPage();
        List<ZyCar> list = zyCarService.selectZyCarList(zyCar);
        return getDataTable(list);
    }

    /**
     * 导出公司车辆列表
     */
    @PreAuthorize("@ss.hasPermi('system:zycar:export')")
    @Log(title = "公司车辆", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ZyCar zyCar)
    {
        List<ZyCar> list = zyCarService.selectZyCarList(zyCar);
        ExcelUtil<ZyCar> util = new ExcelUtil<ZyCar>(ZyCar.class);
        return util.exportExcel(list, "zycar");
    }

    /**
     * 获取公司车辆详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:zycar:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(zyCarService.selectZyCarById(id));
    }

    /**
     * 新增公司车辆
     */
    @PreAuthorize("@ss.hasPermi('system:zycar:add')")
    @Log(title = "公司车辆", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZyCar zyCar)
    {
        return toAjax(zyCarService.insertZyCar(zyCar));
    }

    /**
     * 修改公司车辆
     */
    @PreAuthorize("@ss.hasPermi('system:zycar:edit')")
    @Log(title = "公司车辆", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZyCar zyCar)
    {
        return toAjax(zyCarService.updateZyCar(zyCar));
    }

    /**
     * 删除公司车辆
     */
    @PreAuthorize("@ss.hasPermi('system:zycar:remove')")
    @Log(title = "公司车辆", businessType = BusinessType.DELETE)
	//@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(zyCarService.deleteZyCarByIds(ids));
    }



    @PostMapping("/getList2")
    public String getList2(@RequestBody Map<String,Object> map){
        ZyCar zyCar = JSON.parseObject(JSON.toJSONString(map.get("zyCar")), ZyCar.class);
        List<ZyCar> list = zyCarService.selectZyCarList2(zyCar);
        String jsonString = JSONObject.toJSONString(list);
        return jsonString;
    }

    @PostMapping("/getList")
    public String getList(@RequestBody Map<String,Object> map){
        HttpServletRequest request = ServletUtils.getRequest();
        String ipAddress = HttpUtils.getIpAddress(request);
        /*if(null == ipAddress){
            throw new CustomException("获取请求的ip失败！");
        }else if(!serverip.contains(ipAddress)){
            System.out.println(serverip+"门禁中来自服务器的请求地址是："+ipAddress);
            throw new CustomException("请求地址错误！");
        }*/

        ZyCar zyCar = JSON.parseObject(JSON.toJSONString(map.get("zyCar")), ZyCar.class);
        startPage2(zyCar.getPageNum(),zyCar.getPageSize());
        List<ZyCar> list = zyCarService.selectZyCarList(zyCar);
        TableDataInfo dataTable = getDataTable(list);
        String jsonString = JSONObject.toJSONString(dataTable);
        return jsonString;
    }



    @PostMapping("/addCar")
    public AjaxResult addCar(@RequestBody Map<String,Object> map){
        HttpServletRequest request = ServletUtils.getRequest();
        String ipAddress = HttpUtils.getIpAddress(request);
        /*if(null == ipAddress){
            throw new CustomException("获取请求的ip失败！");
        }else if(!serverip.contains(ipAddress)){
            System.out.println(serverip+"门禁中来自服务器的请求地址是："+ipAddress);
            throw new CustomException("请求地址错误！");
        }*/
        ZyCar zyCar = JSON.parseObject(JSON.toJSONString(map.get("zyCar")), ZyCar.class);
        return toAjax(zyCarService.insertZyCar(zyCar));
    }



    @PostMapping("/editCar")
    public AjaxResult editCar(@RequestBody Map<String,Object> map){
        HttpServletRequest request = ServletUtils.getRequest();
        String ipAddress = HttpUtils.getIpAddress(request);
        /*if(null == ipAddress){
            throw new CustomException("获取请求的ip失败！");
        }else if(!serverip.contains(ipAddress)){
            System.out.println(serverip+"门禁中来自服务器的请求地址是："+ipAddress);
            throw new CustomException("请求地址错误！");
        }*/
        ZyCar zyCar = JSON.parseObject(JSON.toJSONString(map.get("zyCar")), ZyCar.class);
        return toAjax(zyCarService.updateZyCar(zyCar));
    }



    @PostMapping("/delCar")
    public AjaxResult delCar(@RequestBody Map<String,Object> map){
        HttpServletRequest request = ServletUtils.getRequest();
        String ipAddress = HttpUtils.getIpAddress(request);
        /*if(null == ipAddress){
            throw new CustomException("获取请求的ip失败！");
        }else if(!serverip.contains(ipAddress)){
            System.out.println(serverip+"门禁中来自服务器的请求地址是："+ipAddress);
            throw new CustomException("请求地址错误！");
        }*/
        String ids = map.get("ids").toString();
        String[] car_ids = ids.split(",");
        Long[] longs = new Long[car_ids.length];
        for (int i = 0; i < car_ids.length; i++) {
            longs[i] = Long.parseLong(car_ids[i]);
        }
        return toAjax(zyCarService.updateZyCarByIds(longs));
    }



    @PostMapping("/getid")
    public AjaxResult getid(@RequestBody Map<String,Object> map){
        HttpServletRequest request = ServletUtils.getRequest();
        String ipAddress = HttpUtils.getIpAddress(request);
        /*if(null == ipAddress){
            throw new CustomException("获取请求的ip失败！");
        }else if(!serverip.contains(ipAddress)){
            System.out.println(serverip+"门禁中来自服务器的请求地址是："+ipAddress);
            throw new CustomException("请求地址错误！");
        }*/
        String id = map.get("id").toString();
        return AjaxResult.success(zyCarService.selectZyCarById(Long.parseLong(id)));
    }




}
